package com.atwisdom.star.service.business;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import com.atwisdom.star.common.util.SpringContextUtil;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierInstance;
import com.atwisdom.star.core.dao.pojo.modelClass.logic.LogicInstance;
import com.atwisdom.star.core.dsl.modelClass.consume.Impl.ConsumeInstanceDslServiceImpl;
import com.atwisdom.star.core.dsl.modelClass.logic.Impl.LogicInstanceDslServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: LogicConsume
 * @ Description: logic与consume定制化处理操作
 * @author: 王红艳
 * @date:2024/05/15 09:30
 */
@Service
public class LogicConsume extends Business{
    private final LogicInstanceDslServiceImpl containerInstanceService  = SpringContextUtil.getBean(LogicInstanceDslServiceImpl.class);
    private final ConsumeInstanceDslServiceImpl childInstanceService  = SpringContextUtil.getBean(ConsumeInstanceDslServiceImpl.class);
    private LogicInstance containerInstance;
    private List<HierInstance> containerHierInstances;
    private List<JSONObject> childInstances;

    private LogicInstance template;
    private List<HierInstance> templateHierInstances;

    private final String containerModel = "logic";
    private final String childModel = "consume";


    private JSONObject getTemplateStatus(){
        return JsonUtil.buildJson(new Object[] {"if_template",template.getId(),"bf_template",0,"sf_status","创建"});
    }

    private JSONObject getHierTemplateStatus(HierInstance hierItem){
        return JsonUtil.buildJson(new Object[] {"if_template",hierItem.getLinkId(),"bf_template",0});
    }

    /**
     *根据id初始化数据
     * @param id 容器id
     */
    public void setContainer(Long id){
        setContainer(containerInstanceService.queryInsWithHierById(id));
    }

    /**
     *根据id初始化数据
     * @param container 容器实例
     */
    public void setContainer(LogicInstance container){
        this.containerInstance = container;
        containerHierInstances = containerInstance.getHierInstances().stream().filter(hierInstance -> childModel.equals(hierInstance.getLinkModel())).collect(Collectors.toList());
        List<Long> ids = containerHierInstances.stream().map(HierInstance::getLinkId).collect(Collectors.toList());
        setChild(ids);
    }

    /**
     * 根据id初始化模板数据
     * @param id 模板容器id
     */
    public void setTemplate(Long id){
        setTemplate(containerInstanceService.queryInsWithHierById(id));
    }

    /**
     * 根据id初始化模板数据
     * @param template 模板数据
     */
    public void setTemplate(LogicInstance template){
        this.template = template;
        templateHierInstances = template.getHierInstances().stream().filter(hierInstance -> childModel.equals(hierInstance.getLinkModel())).collect(Collectors.toList());
    }


    /**
     * 初始化子节点实例数据
     * @param ids 子节点id集合
     */
    public void setChild(List<Long> ids){
        if(CollectionUtil.isEmpty(ids)){
            return;
        }
        childInstances = childInstanceService.queryByIds(ids);
    }

    /**
     * 复制模板主数据
     * @return 返回复制模板实例
     */
    public JSONObject copyContainerFromTemplate(){
        return logicUtil.copyIns(template.getId(), getTemplateStatus());
    }

    /**
     * 复制模板子数据，并绑定
     * @param copyTemplateIns 复制模板主数据
     * @return 返回复制hier实例集合
     */
    public List<JSONObject> copyHierFromTemplate(JSONObject copyTemplateIns){
        List<HierInstance> hierList = template.getHierInstances().stream().filter(hierInstance -> childModel.equals(hierInstance.getLinkModel())&&null!=hierInstance.getLinkId()).collect(Collectors.toList());
        List<JSONObject> linkList =  new ArrayList<>();
        List<JSONObject> list = hierList.stream().map(item->{
            JSONObject copyIns = consumeUtil.copyIns(item.getLinkId(), getHierTemplateStatus(item));
            linkList.add(JsonUtil.buildJson(new Object[] {"parentId",item.getParentId(),"linkId",copyIns.getLong("id"),"hierValues",new JSONObject()}));
            return copyIns;
        }).collect(Collectors.toList());
        List<String> parentIds = hierList.stream().map(HierInstance::getParentId).collect(Collectors.toList());
        createBatchHier(containerModel, copyTemplateIns.getString("className"), copyTemplateIns.getLong("id"), parentIds, linkList);
        setContainer(copyTemplateIns.getLong("id"));
        return list;
    }
}
